<?php

/* ========================================================================= */
/* This file is part of the HypnoPHP Framework Library                       */
/* Copyright (c) 2009-2010, Radu Buzea                                       */
/* ========================================================================= */

/**
 @author Radu Buzea
 @license GNU Lesser General Public License (www.gnu.org/copyleft/lesser.html)
 @package HypnoPHP
*/

defined('HYPNOPHP_APPLICATION') or
die("Cannot use <i>" . basename(__FILE__) . "</i> outside HypnoPHP Application");

define('H_VERBOSE_MESSAGE', 3);

// ============================================================================
// Class: HUnZip
// ============================================================================

class HCustomFile extends HBaseObject
{

	protected $__file_name = '';
	protected $__file_handle = 0;
	protected $__file_info = array();
	protected $__messages = array();
	protected $__verbose = false;

	# ###########################################################################

	/**
	* Class construct
	*
	* @return HCustomFile
	*/
	public function __construct($file_name = '')
	{
		$this->__messages = array();
		$this->__verbose = false;
		$this->__file_info = array();
		parent::__construct();
	}

	# ###########################################################################

	/**
	* Class destructor
	*
	* @return void
	*/
	public function __destroy()
	{
		$this->close();
	}

	# ###########################################################################

	/**
	* Global members overloading - Getter method
	*
	* @param string $param The object property name
	*/
	public function __get($param = '')
	{
		switch (strtolower(trim($param))) {
			case 'file_name':
			case 'filename':
				return $this->__file_name;
			case 'file_handle':
			case 'filehandle':
				return $this->__file_handle;
			default:
				$file_info = $this->info();
				foreach ($file_info as $key => $value) {
					if ($key == $param) {
						return $value;
					}
				}
		}
		return parent::__get($param);
	}

	# ###########################################################################

	/**
	* Put a message in the messages list, call an error if is needed
	*
	* @param int $msg_type
	* @param string $msg_text
	* @return void
	*/
	protected function __putMessage($msg_type = H_VERBOSE_MESSAGE, $msg_text = '')
	{
		if (!is_array($this->__messages)) {
			$this->__messages = array();
		}
		$this->__messages[] = array(time(), $msg_type, $msg_text);
		if (!$msg_type = H_VERBOSE_MESSAGE) {
			$this->addError($msg_type, $msg_text);
		}
		if ($this->__verbose) {
			echo "<div class=\"hypno_simple_msg\">" . __CLASS__ . " : " . $msg_text . "</div>" . PHP_EOL;
		}
	}

	# ###########################################################################

	/**
	* Open the file
	*
	* @return void
	*/
	public function open($file_name = '', $mode = "r")
	{
		if (!is_file($file_name)) {
			$this->__putMessage(E_USER_ERROR, "Cannot found file <i>{$file_name}</i>.");
			return false;
		}
		if ($this->__file_handle) {
			if ($file_name != "" && $file_name != $this->__file_name) {
				$this->close();
			} else {
				return $this->__file_handle;
			}
		}
		if ($file_name == "") {
			if ($this->__file_name != "") {
				$file_name = $this->__file_name;
			} else {
				$this->__putMessage(E_USER_ERROR, "Empty file name.");
				return false;
			}
		}
		$file_handle = fopen($file_name, $mode);
		if ($file_handle) {
			$this->__file_name = $file_name;
			$this->__file_handle = $file_handle;
			return $file_handle;
		} else {
			$this->__putMessage(E_USER_ERROR, "Failed to open file <i>{$file_name}</i>.");
		}
		return false;
	}

	# ###########################################################################

	/**
	* Close the file
	*
	* @return void
	*/
	public function close()
	{
		if ($this->__file_handle) {
			fclose($this->__file_handle);
			$this->__file_handle = 0;
		}
	}

	# ###########################################################################

	/**
	* Get varoius info about the file
	*
	* @return void
	*/
	public function info()
	{
		if (sizeof($this->__file_info)) { return $this->__file_info; }
		$file_name = $this->__file_name;
		if (!is_file($file_name)) {
			return false;
		}
		$pathinfo = pathinfo($file_name);
		$raw_permission = fileperms($file_name);
		if (($raw_permission & 0xC000) == 0xC000) {
			$permission_info = 's'; // Socket
		} elseif (($perms & 0xA000) == 0xA000) {
			$permission_info = 'l'; // Symbolic Link
		} elseif (($perms & 0x8000) == 0x8000) {
			$permission_info = '-'; // Regular
		} elseif (($perms & 0x6000) == 0x6000) {
			$permission_info = 'b'; // Block special
		} elseif (($perms & 0x4000) == 0x4000) {
			$permission_info = 'd'; // Directory
		} elseif (($perms & 0x2000) == 0x2000) {
			$permission_info = 'c'; // Character special
		} elseif (($perms & 0x1000) == 0x1000) {
			$permission_info = 'p'; // FIFO pipe
		} else {
			$permission_info = 'u'; // Unknown
		}
		// Owner
		$permission_info .= (($perms & 0x0100) ? 'r' : '-');
		$permission_info .= (($perms & 0x0080) ? 'w' : '-');
		$permission_info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
		// Group
		$permission_info .= (($perms & 0x0020) ? 'r' : '-');
		$permission_info .= (($perms & 0x0010) ? 'w' : '-');
		$permission_info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
		// World
		$permission_info .= (($perms & 0x0004) ? 'r' : '-');
		$permission_info .= (($perms & 0x0002) ? 'w' : '-');
		$permission_info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));

		$info = array(
			'accessed_time' => fileatime($file_name),
			'basename' => $pathinfo['basename'],
			'change_time' => filectime($file_name),
			'dirname' => $pathinfo['dirname'],
			'extension' => $pathinfo['extension'],
			'name' => $pathinfo['filename'],
			'modified_time' => filemtime($file_name),
			'permission_info' => $permission_info,
			'raw_permission' => $raw_permission,
			'size' => filesize($file_name),
		);

		return $this->__file_info = $info;
	}

	# ###########################################################################

}
